package com.itheima.controller;

import com.itheima.common.Com.Category;
import com.itheima.common.pojo.Result;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加
     * @param category 对象
     * @return 添加成功
     */
    @PostMapping("")
    public Result save(@RequestBody Category category){
        // todo 没有分类套餐
        categoryService.add(category);
        return Result.success("添加成功");
    }

    /**
     * 修改
     * @param category
     * @return 修改成功
     */
    @PutMapping("")
    public Result update(@RequestBody Category category){

        categoryService.update(category);
        return Result.success("修改成功");
    }

    /**
     * 删除
     * @param id 查到的id
     * @return 删除成功
     */
    @DeleteMapping()
    public Result delete(@RequestParam("id") String id){
        categoryService.delete(id);
        return Result.success("删除");
    }

    /**
     * 查询所有
     * @return 查到的数据
     */
    @GetMapping("/list")
    public Result getall(Integer type){
        List<Category> all = categoryService.getall(type);
        return Result.success(all);
    }

    /**
     * 分页查询
     * @param page 起始页
     * @param pageSize 查多少页
     * @return 查询的数据
     */
    @GetMapping("page")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        return Result.success(categoryService.page(page,pageSize));

    }
}
